package algorithms.question.class08;

import common.util.MyUtil;

/**
 * 盛最多水的容器
 * https://leetcode.com/problems/container-with-most-water/
 *
 * @author guichang
 * @date 2021/8/5
 */

@SuppressWarnings("all")
public class Code2_盛最多水的容器 {
    // 双指针法

    public static void main(String[] args) {
        int[] arr = MyUtil.createArray(1, 8, 6, 2, 5, 4, 8, 3, 7);
        MyUtil.print(maxArea(arr));
    }

    public static int maxArea(int[] h) {
        if (h == null || h.length == 1) {
            return 0;
        }

        int l = 0;
        int r = h.length - 1;
        int max = 0;
        while (l < r) {
            max = Math.max(max, (r - l) * Math.min(h[l], h[r]));
            if (h[l] < h[r]) {
                l++;
            } else {
                r--;
            }
        }
        return max;
    }

}